﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Araman.Controllers
{
    using Araman.Models;
    using Araman.Models.UserModels;
    using Araman.Models.ViewModels;
    using Araman.Resources;
    public class UsersController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            if (Userr.CurrentUser != null)
                return RedirectToAction("Details", "Users", new { userId = Userr.CurrentUser.UserId, userName = Userr.CurrentUser.UserName });
            
            return View(new LoginModel());
        }

        [HttpPost()]
        public ActionResult Login(LoginModel login, FormCollection coll)
        {
            if (Userr.CurrentUser != null)
                return RedirectToAction("Details", "Users", new { userId = Userr.CurrentUser.UserId, userName = Userr.CurrentUser.UserName });

            if (login == null)
                ModelState.AddModelError("LoginUserName", LngViewErrors.Users_Login_ModelNull);

            if (!ModelState.IsValid)
                return View(login);

            Userr user = (from u in DbModel.Current.Users where u.UserName.ToLower() == login.LoginUserName.ToLower() && u.Password == login.LoginPassword select u).FirstOrDefault<Userr>();
            if (user == null)
                ModelState.AddModelError("LoginUserName", LngViewErrors.Users_Login_NoMatch);

            if (!ModelState.IsValid)
                return View(login);

            user.Login();

            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Users/Details/5

        public ActionResult Details(int userId)
        {
            return View();
        }

        //
        // GET: /Users/Create

        public ActionResult Create()
        {
            if (Userr.CurrentUser != null)
                return RedirectToAction("Details", "Users", new  { userId = Userr.CurrentUser.UserId, userName = Userr.CurrentUser.UserName });

            Userr newUser = new Userr();
            return View(newUser);
        }

        //
        // POST: /Users/Create

        [HttpPost]
        public ActionResult Create(Userr newUser, FormCollection collection)
        {
            if (Userr.CurrentUser != null)
                return RedirectToAction("Details", "Users", new  { userId = Userr.CurrentUser.UserId, userName = Userr.CurrentUser.UserName });

            if (newUser == null)
                ModelState.AddModelError("Error-UserNull", new ArgumentNullException("newUser", LngViewErrors.Users_Create_NewUserInfo_Null));

            if (!ModelState.IsValid)            
                return View(newUser);

            newUser.ParentGroup = UserGroup.Group_StandartUsers;
            newUser.Status = UserStatus.EmailNotVerified;
            DbModel.Current.Users.Add(newUser);
            DbModel.Current.SaveChanges();
            return RedirectToAction("Details", "Users", new { userId = newUser.UserId, userName = newUser.UserName });
        }

        //
        // GET: /Users/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Users/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Users/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Users/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
